var app = new Vue({
    el: '.personal',
    data: {
        old: null,//原密码
        newp: null,//新密码
        Confirmnew: null,//确认新密码
        changeunamenum: false,
        newusername:'',
        username:'',
        theuser:'',
        information:'',
        isfalse: false,
        showuseravatar:false,
        showuseravatarr:true,
        loading:false
    },
    computed: {
        onUpload(e) {
            var formData = new FormData();
            formData.append('file', e.target.files[0]);
        },
    },
    created() {
        if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) { //区分iPhone设备  
            document.getElementById('emailsend').style.height="3rem"
            document.getElementById('emailsend').style.width="70%"
            document.getElementById('inputs').style.height="80%"
            document.getElementById('emailsendNumber').style.height="2.8rem"
        }
    },
    mounted(){
        this.loading=true
    },
    methods: {
        getTime: function (time) { //计算时间
            var timeStr = time;
            var t = new Date(timeStr) 
            function charLeftAll(n) {
                if (n < 10) return "0" + n;
                else return n;
            }
            return t.getFullYear() + '-' + charLeftAll((t.getMonth() + 1)) + '-' + charLeftAll(t.getDate()) + '  ' +
                charLeftAll(t.getHours()) + ':' + charLeftAll(t.getMinutes()) + ':' + charLeftAll(t.getSeconds())
        },
        closeinfo() {
            document.getElementsByClassName('errorMessage')[0].style.display="none"
            this.theuser = "";
            this.information = ""
            let BODY = document.getElementsByTagName("body")[0]
            BODY.style.overflowY = ""
        },
        changeusername(){ //更改信息
            this.changeunamenum=!this.changeunamenum;
            let param = {nickname:this.newusername};
            this.istrue = true
            let that=this
            let lang = this.getCookie("lang");
            let reg =  /^[\u4e00-\u9fffa-zA-Z0-9|\S*]{1,8}$/
            if(!this.changeunamenum){
                if(this.newusername==""){
                    let timer=setInterval(() => {
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                        clearInterval(timer)
                        timer=null
                    },2000)
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                    if (lang == "en_US") {
                        this.theuser = "Modify Failed"
                        this.information = "The nickname can not be empty."
                    } else if (lang == "zh_TW") {
                        this.theuser = "修改失敗"
                        this.information = "用戶名不能為空"
                    } else {
                        this.theuser = "修改失败"
                        this.information = "用户名不能为空"
                    }
                    this.istrue = false
                    return
                }else if(!reg.test(this.newusername)){
                    let timer=setInterval(() => {
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                        clearInterval(timer)
                        timer=null
                    },2000)
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                    if (lang == "en_US") {
                        this.theuser="Modify Failed"
                        this.information="The length can not exceed 8 characters and no empty characters."
                    } else if (lang == "zh_TW") {
                        this.theuser="修改失敗"
                        this.information="長度不能超過8個字符且不能有空字符。"
                    } else {
                        this.theuser="修改失败"
                        this.information="长度不能超过8个字符且不能有空字符。"
                    }
                    this.istrue = false
                    return
                }else{
                    axios.post('/user/updatebasicinfo', param).then((res) => {
                        if(res.data.code == 200){
                            this.username=res.data.data.nickname
                            let timer=setInterval(() => {
                                document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                                clearInterval(timer)
                                timer=null
                            },2000)
                            document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                            if (lang == "en_US") {
                                this.theuser="Modify Success"
                                this.information="Your nickname has been modified successfully."
                            } else if (lang == "zh_TW") {
                                this.theuser="修改成功"
                                this.information="您的用戶名修改成功"
                            } else {
                                this.theuser="修改成功"
                                this.information="您的用户名修改成功"
                            }
                            this.newusername=""
                            this.istrue = false
                            return
                        }
                    })
                } 
            }
        },

        getCookie(cookiename) { //获取cookie
            var arr,
              reg = new RegExp("(^| )" + cookiename + "=([^;]*)(;|$)");
            if ((arr = document.cookie.match(reg))) {
              return unescape(arr[2]);
            } else {
              return null;
            }
        },
        updatepassword() { //修改密码
            let lang = this.getCookie("lang");
            let that = this;
            let old = this.$refs.oldpassword.value; //验证吗
            let newp = this.$refs.newpassword.value; //旧密码
            let Confirmnew = this.$refs.Confirmnewpassword.value; //确认新密码
            let UID = (this.locaId = JSON.parse(window.localStorage.getItem("ID")));
            if(old==""){
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser = "Modify Failed";
                    this.information = "Please enter verification code";
                } else if (lang == "zh_TW") {
                    this.theuser = "修改失敗";
                    this.information = "請輸入驗證碼";
                } else {
                    this.theuser = "修改失败";
                    this.information = "请输入验证码";
                }
                this.istrue = false
                return
            }
            if(document.getElementsByClassName("sencode")[0].value.length!=6){
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser = "Modify Failed";
                    this.information = "Invalid verification code format";
                } else if (lang == "zh_TW") {
                    this.theuser = "修改失敗";
                    this.information = "驗證碼格式有誤";
                } else {
                    this.theuser = "修改失败";
                    this.information = "验证码格式有误";
                }
                this.istrue = false
                return
            }
            if (newp == "" || Confirmnew == "") {
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser = "Modify Failed";
                    this.information = "Password cannot be blank Please re-enter";
                } else if (lang == "zh_TW") {
                    this.theuser = "修改失敗";
                    this.information = "密碼不能為空請重新輸入";
                } else {
                    this.theuser = "修改失败";
                    this.information = "密码不能为空请重新输入";
                }
                this.istrue = false
                return
            }
            if(newp==Confirmnew){
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser = "Modify Failed";
                    this.information = "The new password is the same as the original password.";
                } else if (lang == "zh_TW") {
                    this.theuser = "修改失敗";
                    this.information = "新密碼與原密碼相同";
                } else {
                    this.theuser = "修改失败";
                    this.information = "新密码与原密码相同";
                }
                this.istrue = false
                return
            }
            if(Confirmnew.length<6){
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser = "Modify Failed";
                    this.information = "Password length cannot be less than 6";
                } else if (lang == "zh_TW") {
                    this.theuser = "修改失敗";
                    this.information = "密碼長度不能小於6";
                } else {
                    this.theuser = "修改失败";
                    this.information = "密码长度不能小于6";
                }
                this.istrue = false
                return
            }
          
            let params = {
                password: newp,
                newPassword: Confirmnew,
                checkCode: parseFloat(old)
            };
            axios.post("/user/updateUserPassword", params).then(res => {
              if (res.data.code == 200) {
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser = "Modify Success";
                    this.information = "Your password was successfully modified.";
                    } else if (lang == "zh_TW") {
                    this.theuser = "修改成功";
                    this.information = "您的密碼修改成功";
                    } else {
                    this.theuser = "修改成功";
                    this.information = "您的密码修改成功";
                    }
                    this.old = "";
                    this.newp = "";
                    this.Confirmnew = "";
                    location.reload()
                this.istrue = false
                return
                } else if (res.data.code == -10003) {
                    let timer=setInterval(() => {
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                        clearInterval(timer)
                        timer=null
                    },2000)
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                    if (lang == "en_US") {
                        this.theuser = "Modify Failed";
                        this.information = "The original password is wrong please re-enter";
                    } else if (lang == "zh_TW") {
                        this.theuser = "修改失敗";
                        this.information = "原密碼錯誤請重新輸入";
                    } else {
                        this.theuser = "修改失败";
                        this.information = "原密码错误请重新输入";
                    }
                    this.old = "";
                    this.newp = "";
                    this.Confirmnew = "";
                    this.istrue = false
                    return
              }else if(res.data.code == -10007){
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser = "Modify Failed";
                    this.information = "Verification code expired";
                } else if (lang == "zh_TW") {
                    this.theuser = "修改失敗";
                    this.information = "驗證碼過期";
                } else {
                    this.theuser = "修改失败";
                    this.information = "验证码过期";
                }
                this.old = "";
                this.newp = "";
                this.Confirmnew = "";
                this.istrue = false
                return
              }else if(res.data.code == -10009){
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser = "Modify Failed";
                    this.information = "Incorrect verification code";
                  } else if (lang == "zh_TW") {
                    this.theuser = "修改失敗";
                    this.information = "驗證碼輸入錯誤";
                  } else {
                    this.theuser = "修改失败";
                    this.information = "验证码输入错误";
                  }
                this.old = "";
                this.newp = "";
                this.Confirmnew = "";
                this.istrue = false
                return
              }
            });
        },
        signout(){ //退出登录
            axios.post('/user/usersignout',{}).then((res) => {
                if(res.data.code===200){
                    location.href='/'
                    localStorage.clear()
                }
            })
        },
        sendcode() { //发送验证码
            let lang = this.getCookie("lang");
            let params = {
                type: 7,
                sendTo: document.getElementsByClassName("signone")[0].innerText
                };
                axios.post("/comm/mail/send", params).then(res => {  
            });
            this.isfalse = true;
            let time = 60;
            document.getElementsByClassName('emailsendNumber')[0].style.background="#909090"
            let that=this
            let sendtime=setInterval(function(){
                time-=1
                document.getElementsByClassName('emailsendNumber')[0].innerHTML=time +"s"
                if(time==0){
                    if (lang == "en_US") {
                        document.getElementsByClassName('emailsendNumber')[0].innerHTML="send code"
                    } else if (lang == "zh_TW") {
                        document.getElementsByClassName('emailsendNumber')[0].innerHTML="發送"
                    } else {
                        document.getElementsByClassName('emailsendNumber')[0].innerHTML="发送"
                    }
                    document.getElementsByClassName('emailsendNumber')[0].style.background="#CF1132"
                    that.isfalse=false
                    clearInterval(sendtime)
                }
            },1000)
        },
    }
})